package org.feichao.wordinspector;

import java.io.File;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageData;
import com.aspose.words.Node;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;
import com.aspose.words.ShapeType;
import java.util.UUID;

/**
 *
 * 用于生成转成无图格式文档
 * @author Account4Code
 */
public class ExportWord {


    public static String exportWmfFromDoc(File OriginFile) {
        
        String fileName = OriginFile.getName();
        String imgPath = "";
        
        try {
            // 新建文档对象
            Document doc = new Document(OriginFile.getAbsolutePath());
            NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE,true);// 查询文档中所有wmf图片
            Node[] shapes = shapeCollection.toArray();// 序列化
            if (shapes.length > 0) {// 如果文档存在图片
                File file = new File(  ConstVariables.imagePath+fileName.substring(0, fileName.lastIndexOf(".")));
                if (file != null) {
                    if (file.exists() || file.mkdir()) {// 创建文档图片保存文件夹
                        imgPath =  ConstVariables.imagePath+fileName.substring(0,fileName.lastIndexOf("."))+"/";
                    } else {
                        System.out.println("error");
                    }
                }
                for (Node node : shapes) {
                    Shape shape = (Shape) node;
                    if (shape.getShapeType() == ShapeType.IMAGE) {// 如果shape类型是ole类型
                        ImageData i = shape.getImageData();// 获得图片数据
                        String imageName = imageName() + ".png";
                        i.save(imgPath + imageName);// 导出图片
                        File f = new File(imgPath + imageName);
                        if (f.exists()) {
                            DocumentBuilder builder = new DocumentBuilder(doc);// 新建文档节点
                            builder.moveTo(shape);// 移动到图片位置
                            builder.write("[img]"
                                    + fileName.substring(0, fileName
                                            .lastIndexOf("."))
                                    + "/"
                                    + f.getName().substring(0,
                                            f.getName().lastIndexOf("."))
                                    + ".png" + "[/img]");// 插入替换文本
                            shape.remove();// 移除图形
                        } else {
                            System.out.println("error");
                            continue;
                        }
                    }
                }
                String extName = fileName.substring(fileName.lastIndexOf("."));
                String mainName = fileName.substring(0, fileName
                        .lastIndexOf("."));
                doc.save(ConstVariables.doneFilePath + mainName + "_done" + extName);// 保存修改后的文档
                return ConstVariables.doneFilePath+mainName + "_done" + extName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return OriginFile.getAbsolutePath();
    }

    public static String imageName(){
        UUID uuid = UUID.randomUUID();
        return  uuid.toString();
    }

}
